package com.yanqu.road.web.controller.game.forbid;

import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.enums.ePlatformForbiddenType;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.server.manger.ForbiddenIpMgr;

import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2025-02-18
 * @Description : IP封禁
 */

public class IpBanned {

    private ePlatformForbiddenType type;
    private final String ip;
    private final long endTime;

    public IpBanned(String ip, long endTime) {
        this.ip = ip;
        this.endTime = endTime;
    }

    public AdminResult ban() {
        Map<String, Long> forbiddenIpMap = ForbiddenIpMgr.getForbiddenIpMap();
        if (!forbiddenIpMap.containsKey(ip)) {
            if (ServerBussiness.addForbiddenIp(ip, endTime)) {
                return AdminResult.failMsg("添加封禁ip状态失败");
            }
        } else {
            if (ServerBussiness.updateForbiddenIp(ip, endTime)) {
                return AdminResult.failMsg("更新封禁ip状态失败");
            }
        }
        forbiddenIpMap.put(ip, endTime);

        return AdminResult.defaultInstance();
    }

    public AdminResult unban() {
        Map<String, Long> forbiddenIpMap = ForbiddenIpMgr.getForbiddenIpMap();
        if (!forbiddenIpMap.containsKey(ip)) {
            return AdminResult.failMsg("ip " + type.getDesc() + "信息未找到");
        }

        forbiddenIpMap.put(ip, 0L);
        
        return AdminResult.defaultInstance();
    }

    public void clearChat() {

    }
}
